package com.banyou.banyou.customviews;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.banyou.banyou.R;
import com.socks.library.KLog;

/**
 * Created by LX on 2017/3/17.
 */

public class WaveBall extends View {
    private Paint mPaint,mCirclePaint,mTextPaint,mPath2Paint;
    private boolean isShowProgress=true;
    private Context mContext;
    public WaveBall(Context context) {
        super(context);
    }

    public WaveBall(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        mPaint=new Paint();
        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaint.setStrokeWidth(5);
        mPath=new Path();
        mCirclePath=new Path();
        mPath2=new Path();

        mCirclePaint=new Paint();
        mCirclePaint.setColor(Color.BLACK);
        mCirclePaint.setStyle(Paint.Style.STROKE);
        mCirclePaint.setStrokeWidth(5);

        mTextPaint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextSize(100);
        mTextPaint.setStrokeWidth(5);
        mTextPaint.setColor(Color.GRAY);
        mTextRect=new Rect();

        mPath2Paint=new Paint(Paint.ANTI_ALIAS_FLAG);
        mPath2Paint.setStrokeWidth(5);
        mPath2Paint.setShadowLayer(30,0,0,mContext.getResources().getColor(R.color.colorAccent));
        mPath2Paint.setColor(mContext.getResources().getColor(R.color.colorAccent));
        mPath2Paint.setStyle(Paint.Style.STROKE);

    }
    private int mRadius;
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mRadius= (int) (w>h?h*0.8f/2:w*0.8f/2);

        mProgressHeight=getHeight()/2+mRadius+mWaveHeight;
    }

    private Path mPath,mCirclePath,mPath2;
    private int mWaveLength=1000;
    private int mHalfWaveLength=mWaveLength/2;
    private int mWaveHeight=100;
    private int mDx;
    private float mProgressHeight;
    private Rect mTextRect;
    @Override
    protected void onDraw(Canvas canvas) {
        mPath.reset();
        mCirclePath.reset();
        mPath2.reset();
        mCirclePath.addCircle(getWidth()/2,getHeight()/2,mRadius, Path.Direction.CCW);
        mPath.moveTo(-mWaveLength+mDx,mProgressHeight);
        mPath2.moveTo(-mWaveLength+mDx,mProgressHeight);
        for(int i=-mWaveLength;i<=getWidth()+mWaveLength;i+=mWaveLength){
            mPath.rQuadTo(mHalfWaveLength/2,mWaveHeight,mHalfWaveLength,0);
            mPath.rQuadTo(mHalfWaveLength/2,-mWaveHeight,mHalfWaveLength,0);

            mPath2.rQuadTo(mHalfWaveLength/2,mWaveHeight,mHalfWaveLength,0);
            mPath2.rQuadTo(mHalfWaveLength/2,-mWaveHeight,mHalfWaveLength,0);
        }
        mPath.lineTo(getWidth(),getHeight());
        mPath.lineTo(0,getHeight());
        mPath.close();
        canvas.clipPath(mCirclePath);
        canvas.drawPath(mPath,mPaint);
        canvas.drawPath(mPath2,mPath2Paint);
        canvas.drawPath(mCirclePath,mCirclePaint);
        if(isShowProgress){
            mTextPaint.getTextBounds(mTextProgress,0,mTextProgress.length(),mTextRect);
            int height = (int) (mTextRect.height() / 2 + (Math.abs(mTextPaint.ascent()) - mTextPaint.descent()) / 2);
            canvas.drawText(mTextProgress,(float)getWidth()/2-mTextRect.width()/2,(float)getHeight()/2+height/2,mTextPaint);
        }

    }

    /**
     * 开始动画
     */
    public void startAnim(){
        final ValueAnimator anim=ValueAnimator.ofInt(0,mWaveLength);
        anim.setDuration(2000);
        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatCount(ValueAnimator.INFINITE);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mDx= (int) anim.getAnimatedValue();
                invalidate();
            }
        });
        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                mDx=0;
            }
        });
        anim.start();
    }
    private String mTextProgress="0%";

    /**
     * 设置进度
     * @param progress
     */
    public synchronized void setProgress(float progress){
        mTextProgress= (int)((progress)*100)+"%";
        KLog.i("textProgress="+mTextProgress);
        float height=(1-progress)*mRadius*2;
        mProgressHeight=getHeight()/2-mRadius+height;
        if(progress==1){
            mProgressHeight=getHeight()/2-mRadius+height-mWaveHeight;
        }
    }

    public void showProgress(boolean isShow){
        isShowProgress=isShow;
    }
}
